<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="合同名称:" prop="htName">
        <el-input
          v-model="queryParams.htName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号:" prop="htBh">
        <el-input
          v-model="queryParams.htBh"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程状态:" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择流程状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请变更事项:" prop="sqBgSx">
        <el-select v-model="queryParams.sqBgSx" placeholder="请选择申请变更事项" clearable>
          <el-option
            v-for="dict in dict.type.htbg_sqbg_sx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              @row-click="clickRow" ref="table" highlight-current-row v-loading="loading"
              :data="htbgList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column show-overflow-tooltip label="合同名称" align="center" prop="htName" width="300">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.htName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="htBh" width="120"/>
      <el-table-column label="申请日期" align="center" prop="sqTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sqTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申请单位/项目部" align="center" prop="sqDw" />
      <el-table-column label="申请变更事项" show-overflow-tooltip align="center" prop="sqBgSx" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.htbg_sqbg_sx" :value="scope.row.sqBgSx"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="lxPhone" width="150"/>
      <el-table-column label="状态" align="center" prop="processStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
          <el-link type="danger" disabled v-else>{{ '未提交审批' }}</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查询进度 -->
    <Jindu ref="picture"/>

    <!-- 合同变更详情组件 -->
    <HtBgXq ref="htBgXq"/>
  </div>
</template>

<script>
import {
  listHtbg,
} from "@/api/xmgl/htgl/htbg/htbg";
import HtBgDr from "@/views/xmgl/htgl/htbg/components/HtBgDr"
import {getUser} from "@/api/system/user";
import * as huacai from "@/utils/huacai";
import Jindu from "@/views/components/liuChengState/jindu";
import HtBgXq from "@/views/xmgl/htgl/htbg/components/HtBgXq";

export default {
  name: "bgCk", //变更查看页面
  components: {HtBgXq, Jindu, HtBgDr},
  dicts: ['htbg_sqbg_sx', 'rcpt_zc', 'process_business_status'],
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      getHtBgId: null, //从后端获取到的合同变更主键ID
      activeName: 'jbZl', //默认打开的标签
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHtglHtbgFj: [],
      // 组成人员子表选中数据
      checkedHtglHtbgZcry: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同变更表格数据
      htbgList: [],
      // 合同变更附件表格数据
      htglHtbgFjList: [],
      // 变更课题组成人员情况表格数据
      htglHtbgZcryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idWhere: null,
        htName: null,
        htBh: null,
        sqTime: null,
        sqDw: null,
        lxPhone: null,
        sqBgSx: null,
        userId: null,
        deptId: null,
        processStatus: null,
      },
      // 表单参数
      form: {},
      deptLable: null, //当前登录用户的详细部门名称
    };
  },
  created() {
    this.getList();
    this.getDeptName()
  },
  methods: {
    // 查询详情信息
    handleButtonClick(row) {
      this.$refs.htBgXq.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    // 获取当前登录用户的部门详细名称(包含上级名称)
    getDeptName() {
      let getUserId = this.$store.state.user.id //当前登录用户的用户ID
      getUser(getUserId).then(res => {
        huacai.getFullDeptLabel(res.data.dept.deptId).then(fullLabel => {
          this.deptLable = fullLabel; // 获取当前登录用户的部门详细名称(包含上级名称)
        });
      })
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    /** 查询合同变更列表 */
    getList() {
      this.loading = true;
      listHtbg(this.queryParams).then(response => {
        this.htbgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.htbgId)
      if (ids.length === 1) {
        this.ids = ids[0]
      }else {
        this.ids = ids
      }
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('htbg/htbg/export', {
        ...this.queryParams
      }, `htbg_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
